package com.nxg.pm04;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.nxg.pm04.baseAdpater.PMBaseAdapter;
import com.nxg.pm04.util.ReflushStateListener;
import com.nxg.pm04.util.URLDTO;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MainActivity extends AppCompatActivity {
    TextView activeDate;//活跃日期
    Spinner assetName;//名称下拉框
    Spinner taskName; //任务名称下拉框
    ImageView addPM;//添加按钮
    TextView cleatFilter;//清除按钮

    Calendar calendar;//日历
    //@SuppressLint("HandlerLeak")注解实际作用不是很大，就直接删除了这个注解，然后编译执行
    //@SuppressLint("HandlerLeak") //该注解时干什么用的？？？？？
    private Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                //1表示消息发送成功，
                case  1:{
                    try{
                        //将消息转换为jsonObject格式的
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        //JSON数组形式
                        JSONArray jsonArray = jsonObject.getJSONArray("infoList");
                        PMBaseAdapter pmBaseAdapter = new PMBaseAdapter(MainActivity.this,jsonArray,activeDate.getText().toString(),reflushStateListener);
                        //获取 列表视图 id
                      ListView listView = findViewById(R.id.listPM);
                        //向列表中添加 基本适配器
                      listView.setAdapter(pmBaseAdapter);
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    break;
                }

            }
        }
    };

    /**
     * onCreate方法是重写父类Activity的onCreate方法而实现的。
     * onCreate方法用来初始化Activity实例对象。
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //调用其父类Activity的onCreate方法来实现对界面的图画绘制工作。
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        calendar = Calendar.getInstance();
        //新版本的不需要进行转换
        activeDate = findViewById(R.id.activeDate);
        addPM = findViewById(R.id.addListPM);
        cleatFilter =findViewById(R.id.clearFilter);
        assetName = findViewById(R.id.assetNameSpinner);
        taskName = findViewById(R.id.taskNameSpinner);

        //日期格式处理
        //String date = String.format("%d-%d-%d",calendar.get(calendar.YEAR),calendar.get(calendar.MONTH)+1,calendar.get(calendar.DAY_OF_MONTH));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String activeDateStr =  sdf.format(new Date());
        activeDate.setText(activeDateStr);//将当前获取的日期写入到结束日期中
        URLDTO.findByAllPM(assetName.getSelectedItem().toString(),taskName.getSelectedItem().toString(),activeDate.getText().toString(),handler);
        //URLDTO.findByAllPMTest(assetName.getSelectedItem().toString(),taskName.getSelectedItem().toString(),handler);
        //结束日期 文本内容变化监听
        activeDate.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                //URLDTO.findByAllPM(assetName.getSelectedItem().toString(),taskName.getSelectedItem().toString(),activeDate.getText().toString(),handler);
                URLDTO.findByAllPMTest(assetName.getSelectedItem().toString(),taskName.getSelectedItem().toString(),handler);
            }
        });
        //添加按钮，增加点击监听事件，
        addPM.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //显示跳转到下一个活动
                Intent intent = new Intent(MainActivity.this,CreatePMActivity.class);
                //startActivity方法
                startActivity(intent);
            }
        });
        //下拉框内容监听
        assetName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            //选中的商品
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //URLDTO.findByAllPM(assetName.getSelectedItem().toString(),taskName.getSelectedItem().toString(),activeDate.getText().toString(),handler);
                URLDTO.findByAllPMTest(assetName.getSelectedItem().toString(),taskName.getSelectedItem().toString(),handler);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        //任务  设置商品选中监听
        taskName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                 //URLDTO.findByAllPM(assetName.getSelectedItem().toString(),taskName.getSelectedItem().toString(),activeDate.getText().toString(),handler);
                 URLDTO.findByAllPMTest(assetName.getSelectedItem().toString(),taskName.getSelectedItem().toString(),handler);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        //清除按钮，设置点击监听事件
        cleatFilter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                assetName.setSelection(0);
                taskName.setSelection(0);
                 //URLDTO.findByAllPM(assetName.getSelectedItem().toString(),taskName.getSelectedItem().toString(),activeDate.getText().toString(),handler);
                 URLDTO.findByAllPMTest(assetName.getSelectedItem().toString(),taskName.getSelectedItem().toString(),handler);
            }
        });
        //重刷新监听事件
        reflushStateListener = new ReflushStateListener() {
            //重写监听事件
            @Override
            public void reflush(int id) {
                if(id == 1){
                  //URLDTO.findByAllPM(assetName.getSelectedItem().toString(),taskName.getSelectedItem().toString(),activeDate.getText().toString(),handler);

                  URLDTO.findByAllPMTest(assetName.getSelectedItem().toString(),taskName.getSelectedItem().toString(),handler);
                }
            }
        };

    }
    //重新启动
    @Override
    protected void onRestart() {
        super.onRestart();//看到onRestart仅在活动进入onStop状态时被调用
        //URLDTO.findByAllPM(assetName.getSelectedItem().toString(),taskName.getSelectedItem().toString(),activeDate.getText().toString(),handler);
        //
       URLDTO.findByAllPMTest(assetName.getSelectedItem().toString(),taskName.getSelectedItem().toString(),handler);

    }

    //自定义  刷新监听器
   private ReflushStateListener reflushStateListener;
    //生成setget方法
    public ReflushStateListener getReflushStateListener() {
        return reflushStateListener;
    }

    public void setReflushStateListener(ReflushStateListener reflushStateListener) {
        this.reflushStateListener = reflushStateListener;
    }

}